<?php
#####################################
# * @Author: lehieu008              #
# * @Email: lehieu008@gmail.com     #
# * @Copyright: 2008 - 2010         #
#####################################
if(!defined('BASEPATH'))exit('No direct script access allowed');
class Menu_module extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {
        #BEGIN: getVar
        $segments = array('section', 'category');
        $getVar = $this->uri->uri_to_assoc(2, $segments);
        #END getVar
        #BEGIN: Section
        if(isset($getVar['section']) && $getVar['section'] !== FALSE && (int)$getVar['section'] > 0)
        {
            $data['sectionSelected'] = $getVar['section'];
            #BEGIN: Category
            if(isset($getVar['category']) && $getVar['category'] !== FALSE && (int)$getVar['category'] > 0)
            {
                $data['categorySelected'] = $getVar['category'];
            }
            #END Category
        }
        #END Section
        switch(trim(strtoupper($this->uri->segment(1))))
        {
            case 'PRODUCT':
                $data['title'] = lang('PRODUCT_MENU');
                $data['sections'] = $this->global_model->fetch("SELECT secID, secName FROM {PRE}section WHERE secType = 1 AND secStatus = 1 ORDER BY secOrder ASC", true);
                $data['categories'] = $this->global_model->fetch("SELECT catID, catName FROM {PRE}category WHERE catStatus = 1 AND catSection IN(SELECT secID FROM {PRE}section WHERE secType = 1 AND secStatus = 1) ORDER BY catOrder ASC", true);
                break;
            case 'ADS':
                $data['title'] = lang('ADS_MENU');
                $data['sections'] = $this->global_model->fetch("SELECT secID, secName FROM {PRE}section WHERE secType = 2 AND secStatus = 1 ORDER BY secOrder ASC", true);
                $data['categories'] = $this->global_model->fetch("SELECT catID, catName FROM {PRE}category WHERE catStatus = 1 AND catSection IN(SELECT secID FROM {PRE}section WHERE secType = 2 AND secStatus = 1) ORDER BY catOrder ASC", true);
                break;
            case 'SHOP':
                $data['title'] = lang('SHOP_MENU');
                $data['sections'] = $this->global_model->fetch("SELECT secID, secName FROM {PRE}section WHERE secType = 3 AND secStatus = 1 ORDER BY secOrder ASC", true);
                $data['categories'] = $this->global_model->fetch("SELECT catID, catName FROM {PRE}category WHERE catStatus = 1 AND catSection IN(SELECT secID FROM {PRE}section WHERE secType = 3 AND secStatus = 1) ORDER BY catOrder ASC", true);
                break;
            case 'JOB':
                $data['title'] = lang('JOB_MENU');
                $data['sections'] = $this->global_model->fetch("SELECT secID, secName FROM {PRE}section WHERE secType = 4 AND secStatus = 1 ORDER BY secOrder ASC", true);
                $data['categories'] = $this->global_model->fetch("SELECT catID, catName FROM {PRE}category WHERE catStatus = 1 AND catSection IN(SELECT secID FROM {PRE}section WHERE secType = 4 AND secStatus = 1) ORDER BY catOrder ASC", true);
                break;
            case 'EMPLOY':
                $data['title'] = lang('EMPLOY_MENU');
                $data['sections'] = $this->global_model->fetch("SELECT secID, secName FROM {PRE}section WHERE secType = 4 AND secStatus = 1 ORDER BY secOrder ASC", true);
                $data['categories'] = $this->global_model->fetch("SELECT catID, catName FROM {PRE}category WHERE catStatus = 1 AND catSection IN(SELECT secID FROM {PRE}section WHERE secType = 4 AND secStatus = 1) ORDER BY catOrder ASC", true);
                break;
            case 'NEWS':
                $data['title'] = lang('NEWS_MENU');
                $data['sections'] = $this->global_model->fetch("SELECT secID, secName FROM {PRE}section WHERE secType = 5 AND secStatus = 1 ORDER BY secOrder ASC", true);
                $data['categories'] = $this->global_model->fetch("SELECT catID, catName FROM {PRE}category WHERE catStatus = 1 AND catSection IN(SELECT secID FROM {PRE}section WHERE secType = 5 AND secStatus = 1) ORDER BY catOrder ASC", true);
                break;
            case 'CART':
                $data['title'] = lang('PRODUCT_MENU');
                $idProducts = array();
                foreach($this->cart->contents() as $cart)
                {
                    $idProducts[] = $cart['id'];
                }
                if(count($idProducts) > 0)
                {
                    $idProducts = implode(',', $idProducts);
                    $data['sections'] = $this->global_model->fetch("SELECT secID, secName FROM {PRE}section WHERE secStatus = 1 AND secID IN(SELECT proSection FROM {PRE}product WHERE proID IN($idProducts) GROUP BY proSection) ORDER BY secOrder ASC", true);
                    $data['categories'] = $this->global_model->fetch("SELECT catID, catName FROM {PRE}category WHERE catStatus = 1 AND catSection IN(SELECT secID FROM {PRE}section WHERE secStatus = 1 AND secID IN(SELECT proSection FROM {PRE}product WHERE proID IN($idProducts) GROUP BY proSection)) ORDER BY catOrder ASC", true);
                }
                else
                {
                    $data['sections'] = $this->global_model->fetch("SELECT secID, secName FROM {PRE}section WHERE secType = 1 AND secStatus = 1 ORDER BY secOrder ASC LIMIT 0,1", true);
                    $idSections = array();
                    foreach($data['sections'] as $section)
                    {
                        $idSections[] = $section->secID;
                    }
                    if(count($idSections) > 0)
                    {
                        $idSections = implode(',', $idSections);
                        $data['categories'] = $this->global_model->fetch("SELECT catID, catName FROM {PRE}category WHERE catStatus = 1 AND catSection IN($idSections) ORDER BY catOrder ASC", true);
                    }
                }
                break;
            case 'ACCOUNT':
                $data['title'] = lang('ACCOUNT_MENU');
                break;
            default:
                $data['title'] = lang('PRODUCT_MENU');
                $data['sections'] = $this->global_model->fetch("SELECT secID, secName FROM {PRE}section WHERE secType = 1 AND secStatus = 1 ORDER BY secOrder ASC", true);
                $data['categories'] = $this->global_model->fetch("SELECT catID, catName FROM {PRE}category WHERE catStatus = 1 AND catSection IN(SELECT secID FROM {PRE}section WHERE secType = 1 AND secStatus = 1) ORDER BY catOrder ASC", true);
        }
        #Load View
        $this->load->view('menu/defaults', $data);
    }
}